package love.yuanmengrobot.wuziqi.UI;/*
 * Created by JFormDesigner on Sun May 09 16:28:46 CST 2021
 */


import love.yuanmengrobot.wuziqi.UIlzl.Scoreboard;
import love.yuanmengrobot.wuziqi.UIyc.ScoreWin;
import love.yuanmengrobot.wuziqi.UIyqh.MallForm;
import love.yuanmengrobot.wuziqi.Util.ChessBoard;
import love.yuanmengrobot.wuziqi.Util.JdbcUtil;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.URL;
import java.sql.*;
import java.util.*;
import java.util.List;
import javax.swing.*;

import static java.lang.Thread.sleep;


/**
 * @author 1
 */
public class WuZiQiClient extends JFrame implements Runnable {
    public WuZiQiClient() {

//        colorSen[0] = new Color(254, 0, 0);
//        colorSen[1] = new Color(253, 166, 0);
//        colorSen[2] = new Color(255, 255, 1);
//        colorSen[3] = new Color(0, 128, 1);
//        colorSen[4] = new Color(63, 224, 208);
//        colorSen[5] = new Color(64, 0, 151);
//        colorSen[6] = new Color(148, 0, 212);
//        try {
//            socket = new Socket("127.0.0.1", 9999);
//            System.out.println("已经连上服务器了11");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//
//        wuZiQiClient = new WuZiQiClient();
//        wuZiQiClient.readMeg.start();
//
//        startGame = new StartGame(socket);
//
//
//        wuZiQiClient.setVisible(false);
//        startGame.setVisible(true);
        initComponents();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panelProp = new JPanel();
        spinner1Kill = new JSpinner();
        spinner2Discolor = new JSpinner();
        spinner3Bomb = new JSpinner();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        label8 = new JLabel();
        label9 = new JLabel();
        label10 = new JLabel();
        label11 = new JLabel();
        label12 = new JLabel();
        label13 = new JLabel();
        label14 = new JLabel();
        button1AckProp = new JButton();
        label1TimeText = new JLabel();

        //======== this ========
        setIconImage(null);
        Container contentPane = getContentPane();
//        contentPane.setLayout(null);

        //======== panelProp ========
        {
            panelProp.setBackground(Color.white);
            panelProp.setLayout(null);

            //---- spinner1Kill ----
            spinner1Kill.setModel(new SpinnerNumberModel(0, 0, 3, 1));
            panelProp.add(spinner1Kill);
            spinner1Kill.setBounds(200, 225, 68, 34);

            //---- spinner2Discolor ----
            spinner2Discolor.setModel(new SpinnerNumberModel(0, 0, 3, 1));
            panelProp.add(spinner2Discolor);
            spinner2Discolor.setBounds(330, 225, 68, 34);

            //---- spinner3Bomb ----
            spinner3Bomb.setModel(new SpinnerNumberModel(0, 0, 1, 1));
            panelProp.add(spinner3Bomb);
            spinner3Bomb.setBounds(455, 225, 68, 34);

            //---- label4 ----
            label4.setText("\u706d\u68cb");
            panelProp.add(label4);
            label4.setBounds(new Rectangle(new Point(220, 265), label4.getPreferredSize()));

            //---- label5 ----
            label5.setText("\u4ece\u68cb\u76d8\u4e0a\u53bb\u6389");
            panelProp.add(label5);
            label5.setBounds(new Rectangle(new Point(200, 295), label5.getPreferredSize()));

            //---- label6 ----
            label6.setText("\u4efb\u610f\u4e00\u4e2a\u68cb\u5b50");
            panelProp.add(label6);
            label6.setBounds(new Rectangle(new Point(200, 325), label6.getPreferredSize()));

            //---- label7 ----
            label7.setText("\u6539\u53d8\u68cb\u76d8\u4e0a");
            panelProp.add(label7);
            label7.setBounds(330, 295, 72, 17);

            //---- label8 ----
            label8.setText("\u53d8\u8272\u68cb");
            panelProp.add(label8);
            label8.setBounds(345, 265, 45, 17);

            //---- label9 ----
            label9.setText("\u4efb\u610f\u4e00\u4e2a\u68cb\u5b50\u7684\u989c\u8272");
            panelProp.add(label9);
            label9.setBounds(310, 325, 130, 17);

            //---- label10 ----
            label10.setText("\u9009\u62e9\u4e00\u4e2a\u81ea\u5df1\u7684\u68cb\u5b50");
            panelProp.add(label10);
            label10.setBounds(450, 295, 135, 17);

            //---- label11 ----
            label11.setText("\u70b8\u68cb");
            panelProp.add(label11);
            label11.setBounds(480, 265, 55, 17);

            //---- label12 ----
            label12.setText("\u8ba9\u68cb\u5b50\u5468\u56f4\u4e00\u5708\u7684\u68cb\u5b50");
            panelProp.add(label12);
            label12.setBounds(445, 325, 160, 17);

            //---- label13 ----
            label13.setText("\u5305\u62ec\u81ea\u5df1\u7684\u68cb\u5b50");
            panelProp.add(label13);
            label13.setBounds(460, 375, 105, label13.getPreferredSize().height);

            //---- label14 ----
            label14.setText("\u4ece\u68cb\u76d8\u4e0a\u53bb\u6389");
            panelProp.add(label14);
            label14.setBounds(465, 350, 95, label14.getPreferredSize().height);

            //---- button1AckProp ----
            button1AckProp.setText("\u786e\u5b9a");
            button1AckProp.setBackground(new Color(222, 222, 222));
            panelProp.add(button1AckProp);
            button1AckProp.setBounds(new Rectangle(new Point(335, 390), button1AckProp.getPreferredSize()));

            //---- label1TimeText ----
            label1TimeText.setText("\u5373\u5c06\u5f00\u59cb\uff1a");
            panelProp.add(label1TimeText);
            label1TimeText.setBounds(220, 15, 340, 45);
        }
        contentPane.add(panelProp);
        panelProp.setBounds(0, 40, 760, 700);

        contentPane.setPreferredSize(new Dimension(735, 555));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents

        chessBoard = new ChessBoard(socket);//初始化面板对象，创建和添加菜单


        ////////////////
        MyItemListener lis = new MyItemListener();//初始化按钮事件监听器内部类


        contentPane.add(chessBoard);
        chessBoard.setOpaque(true);
//        ClassLoader loader = StartChessJFrame.class.getClassLoader(); //加载图片
//        URL url_1 = loader.getResource("player1.jpg");
//        ImageIcon player1 = new ImageIcon(url_1);//玩家1头像
//        URL url_2 = loader.getResource("player2.jpg");
//        ImageIcon player2 = new ImageIcon(url_2);//玩家2头像
        leftbar = new JPanel();
//        playerbar0 = new JPanel();
//        playerbar1 = new JPanel();
//        playerbar2 = new JPanel();
//        playerbar3 = new JPanel();
//        playerbar4 = new JPanel();
//        playerbar5 = new JPanel();
//        playerbar6 = new JPanel();
//        playerbar7 = new JPanel();
//        playerbar0.setLayout(new GridLayout(1, 1));//1行一列
//        playerbar1.setLayout(new GridLayout(1, 1));//1行一列
//        playerbar2.setLayout(new GridLayout(1, 1));//1行一列
//        playerbar3.setLayout(new GridLayout(1, 1));//1行一列
//        playerbar4.setLayout(new GridLayout(1, 1));//1行一列
//        playerbar5.setLayout(new GridLayout(1, 1));//1行一列
//        playerbar6.setLayout(new GridLayout(1, 1));//1行一列
//        playerbar7.setLayout(new GridLayout(1, 1));//1行一列
//        JPanel playerPanel0 = new JPanel(new GridLayout(1, 1, 5, 5));//玩家头像总区域
//        JPanel playerPanel1 = new JPanel(new GridLayout(1, 1, 5, 5));
//        JPanel playerPanel2 = new JPanel(new GridLayout(1, 1, 5, 5));
//        JPanel playerPanel3 = new JPanel(new GridLayout(1, 1, 5, 5));
//        JPanel playerPanel4 = new JPanel(new GridLayout(1, 1, 5, 5));
//        JPanel playerPanel5 = new JPanel(new GridLayout(1, 1, 5, 5));
//        JPanel playerPanel6 = new JPanel(new GridLayout(1, 1, 5, 5));
//        JPanel playerPanel7 = new JPanel(new GridLayout(1, 1, 5, 5));
////        JLabel player1Label = new JLabel(player1);
////        JLabel player2Label = new JLabel(player2);
////        playerPanel1.add(player1Label);//玩家1
////        playerPanel2.add(player2Label);//玩家2
//        playerbar0.add(playerPanel0);
//        playerbar1.add(playerPanel1);
//        playerbar2.add(playerPanel2);
//        playerbar3.add(playerPanel3);
//        playerbar4.add(playerPanel4);
//        playerbar5.add(playerPanel5);
//        playerbar6.add(playerPanel6);
//        playerbar7.add(playerPanel7);
        player0name = new JLabel("    		       ");
        player1name = new JLabel("    		       ");
        player2name = new JLabel("    		       ");
        player3name = new JLabel("    		       ");
        player4name = new JLabel("    		       ");
        player5name = new JLabel("    		       ");
        player6name = new JLabel("    	          ");
        player7name = new JLabel("******积分******");
        leftbar.setLayout(new GridLayout(8, 1, 5, 5));//8行一列
//        leftbar.add(playerbar7);
        leftbar.add(player7name);
//        leftbar.add(playerbar0);
        leftbar.add(player0name);
//        leftbar.add(playerbar1);
        leftbar.add(player1name);
//        leftbar.add(playerbar2);
        leftbar.add(player2name);
//        leftbar.add(playerbar3);
        leftbar.add(player3name);
//        leftbar.add(playerbar4);
        leftbar.add(player4name);
//        leftbar.add(playerbar5);
        leftbar.add(player5name);
//        leftbar.add(playerbar6);
        leftbar.add(player6name);


        leftbar.setBorder(BorderFactory.createEmptyBorder(30, 5, 18, 10));
        toolbar = new JPanel();//工具面板栏实例化
        toolbar.setBorder(BorderFactory.createEmptyBorder(0, 25, 0, 30));
//        startButton = new Button("重新开始");
//        backButton = new Button("悔棋");
//        exitButton = new Button("退出");//三个按钮初始化


        //道具按钮
        killButton = new JButton("灭棋");
        killButton.setBackground(new Color(222, 222, 222));
        discolorButton = new JButton("变色棋");
        discolorButton.setBackground(new Color(222, 222, 222));
        bombButton = new JButton("炸棋");
        bombButton.setBackground(new Color(222, 222, 222));
        yuanButton = new JButton("原色");
        yuanButton.setBackground(new Color(222, 222, 222));

        yuanButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                chessBoard.setCurrentChessAttribute(0);
                System.out.println("当前选择原色棋子");
            }
        });
        killButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if (chessBoard.getKillNum() == 0) {
                    System.out.println("灭棋没有了哦");
                }
                chessBoard.setCurrentChessAttribute(1);
                System.out.println("当前选择杀棋");
            }
        });
        discolorButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (chessBoard.getDisNum() == 0) {
                    System.out.println("变色棋没有了哦");
                } else
                    chessBoard.setCurrentChessAttribute(2);
                System.out.println("当前选择变色棋");
            }
        });
        bombButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (chessBoard.getBlackTime() == 0) {
                    System.out.println("炸棋没有了哦");
                } else
                    chessBoard.setCurrentChessAttribute(3);
                System.out.println("当前选择炸棋");
            }
        });
        toolbar.setLayout(new FlowLayout(FlowLayout.RIGHT));//将工具面板按钮用FlowLayout布局
        chessBoard.setLayout(new BorderLayout());
//        toolbar.add(backButton);
//        toolbar.add(startButton);
//        toolbar.add(exitButton);//个按钮添加到工具面板上

        toolbar.add(killButton);
        toolbar.add(discolorButton);
        toolbar.add(bombButton);
        toolbar.add(yuanButton);


//        startButton.addActionListener(lis);
//        backButton.addActionListener(lis);
//        exitButton.addActionListener(lis);//将三个按钮事件注册监听事件
        add(leftbar, BorderLayout.WEST);
        add(toolbar, BorderLayout.SOUTH);//将工具面板布局到界面南方也就是下面
        add(chessBoard, BorderLayout.CENTER);//将面板对象添加到窗体上


        ////////////////


//        panelGame.add(chessBoard);
//        panelGame.setVisible(true);
        chessBoard.setOpaque(true);


        chessBoard.setLayout(new BorderLayout());

        add(chessBoard, BorderLayout.CENTER);//将面板对象添加到窗体上


        button1AckProp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int killNum = Integer.parseInt(spinner1Kill.getValue().toString());
                int disNum = Integer.parseInt(spinner2Discolor.getValue().toString());
                int bombNum = Integer.parseInt(spinner3Bomb.getValue().toString());


                if (bombNum == 0 && disNum == 0 && killNum == 0) return;
                //添加使用道具的JDBC代码
//  ///////              username、bombNum、disNum、killNum
///////////
                int num1, num2, num3;

//                num1 = Integer.parseInt(spinner1.getValue().toString());
//                num2 = Integer.parseInt(spinner2.getValue().toString());
//                num3 = Integer.parseInt(spinner3.getValue().toString());

                boolean check1 = QueryNum(1, killNum), check2 = QueryNum(2, disNum), check3 = QueryNum(3, bombNum);

                if (check1 && check2 && check3) {
                    //输入地址、账号密码，加载驱动，输入sql语句，开始查询，遍历查询结果。
                    Connection conn = null;
                    String url = "jdbc:oracle:thin:@112.74.190.130:1521:orcl";
                    PreparedStatement stmt = null;//SQL语句对象
                    String sql = " UPDATE wuziqiuserprops SET propnum= CASE propid " +
                            "WHEN 'P01' THEN propnum-? " +
                            "WHEN 'P02' THEN propnum-? " +
                            "WHEN 'P03' THEN propnum-? " +
                            "END WHERE userid =?";

                    try {
//                            Class.forName("oracle.jdbc.driver.OracleDriver");
                        conn = JdbcUtil.getConnection();
                        stmt = conn.prepareStatement(sql);
                        stmt.setInt(1, killNum);
                        stmt.setInt(2, disNum);
                        stmt.setInt(3, bombNum);
                        stmt.setString(4, startGame.getUserLoginId());
                        stmt.executeUpdate();


                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    } finally {
                        //释放资源：数据库连接很昂贵
//                            System.out.println("选择成功！");

//                                stmt.close();
//                                conn.close();//关连接
                        JdbcUtil.close(stmt, conn);

                    }
                    chessBoard.setBombNum(bombNum);
                    chessBoard.setDisNum(disNum);
                    chessBoard.setKillNum(killNum);
                    panelProp.setVisible(false);
                    chessBoard.setVisible(true);
                } else {
                    String Meg = null;
//                        System.out.print("选择失败：");
                    Meg = "选择失败：";
                    if (!check1) Meg += "杀棋数量不足 ";
                    if (!check2) Meg += "变色棋数量不足 ";
                    if (!check3) Meg += "炸棋数量不足";
                    JOptionPane.showMessageDialog(null, Meg);

                }

            }
        });

        chessBoard.setVisible(false);
        panelProp.setVisible(true);
        panelProp.setOpaque(false);

//        panelProp.setBounds();
        panelProp.setBounds(0, 40, 900, 660);
        setSize(760, 700);
        setResizable(false);// 设置窗体不可以放大
        setVisible(false);
        //      第30行  contentPane.setLayout(null);//该行不注释，棋盘不显示


        //开始左上角图标更换
//        URL ur3=Example1.class.getResource("读取.png");
//        Icon Icon3=new ImageIcon(ur3);
//        Rf.setIcon(Icon3);
//        URL urlAvatar = MD5.class.getResource("Avatar.jpg");
//        System.out.println(urlAvatar.toString());
//        this.setIconImage(new ImageIcon("Avatar.jpg").getImage());
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panelProp;
    private JSpinner spinner1Kill;
    private JSpinner spinner2Discolor;
    private JSpinner spinner3Bomb;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private JLabel label8;
    private JLabel label9;
    private JLabel label10;
    private JLabel label11;
    private JLabel label12;
    private JLabel label13;
    private JLabel label14;
    private JButton button1AckProp;
    private JLabel label1TimeText;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
    List<love.yuanmengrobot.wuziqi.Util.Point> chessArrayList;
    static Color colorSen[] = new Color[7];
    static String ColorName[] = new String[7];
    public ChessBoard chessBoard;//对战面板
    private Button startButton;//设置开始按钮
    private Button backButton;//设置悔棋按钮
    private Button exitButton;//设置退出按钮
    //道具
    private JButton killButton;//设置杀棋按钮
    private JButton discolorButton;//设置变色按钮
    private JButton bombButton;//设置炸棋子按钮
    private JButton yuanButton;//设置棋子为原色

    private JPanel toolbar;//工具条面板
    //    private JButton startButton;//设置开始按钮
//    private JButton backButton;//设置悔棋按钮
//    private JButton exitButton;//设置退出按钮
    private JPanel leftbar;  //设置左面板，包括头像以及名称
    private JPanel playerbar0;//玩家头像
    private JPanel playerbar1;//玩家头像
    private JPanel playerbar2;//玩家头像
    private JPanel playerbar3;//玩家头像
    private JPanel playerbar4;//玩家头像
    private JPanel playerbar5;//玩家头像
    private JPanel playerbar6;//玩家头像
    private JPanel playerbar7;//玩家头像

    JLabel player0name;
    JLabel player1name;
    JLabel player2name;
    JLabel player3name;
    JLabel player4name;
    JLabel player5name;
    JLabel player6name;
    JLabel player7name;


    public static Socket socket;
    public Map<Integer, Integer> mapScore = new HashMap<Integer, Integer>();
    static StartGame startGame;
    static WuZiQiClient wuZiQiClient;
    static StrtForm strtForm;


    Thread readMeg = new Thread(this);//倒计时线程

    public boolean QueryNum(int num, int amount) {
        int toamount = 0;

        PreparedStatement stmt = null;//SQL语句对象
        Connection conn = null;
        ResultSet rs = null;
        String sql = "SELECT propnum FROM wuziqiuserprops WHERE userid=? AND propid=?";

        try {
            conn = JdbcUtil.getConnection();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        try {
            stmt = conn.prepareStatement(sql);
            switch (num) {
                case 1:
                    stmt.setString(2, "P01");
                    break;
                case 2:
                    stmt.setString(2, "P02");
                    break;
                case 3:
                    stmt.setString(2, "P03");
                    break;
                default:
                    break;
            }
            stmt.setString(1, startGame.getUserLoginId());


            rs = stmt.executeQuery();
            rs.next();
            toamount = Integer.parseInt(rs.getString(1));

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //释放资源：数据库连接很昂贵
            try {
//                stmt.close();
                conn.close();//关连接
                JdbcUtil.close(rs, stmt, conn);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (toamount >= amount) return true;
        else return false;
    }

    private class MyItemListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            Object obj = e.getSource();//获取事件源
            if (obj == startButton) {
                System.out.println("重新开始...");//重新开始
                chessBoard.restartGame();
            } else if (obj == exitButton) {
                System.exit(0);//结束应用程序
            } else if (obj == backButton) {
                System.out.println("悔棋...");//悔棋
                chessBoard.goback();
            }
        }
    }

    //

    public static void main(String[] args) {


        colorSen[0] = new Color(254, 0, 0);
        colorSen[1] = new Color(253, 166, 0);
        colorSen[2] = new Color(255, 255, 1);
        colorSen[3] = new Color(0, 128, 1);
        colorSen[4] = new Color(63, 224, 208);
        colorSen[5] = new Color(64, 0, 151);
        colorSen[6] = new Color(148, 0, 212);
        String colorIs = null;
        ColorName[0] = "红色棋";
        ColorName[1] = "橙色棋";
        ColorName[2] = "金色棋";
        ColorName[3] = "绿色棋";
        ColorName[4] = "蓝色棋";
        ColorName[5] = "靛色棋";
        ColorName[6] = "紫色棋";


        try {
            socket = new Socket("127.0.0.1", 9999);
            System.out.println("客户端连上服务器了");
        } catch (IOException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "连不上服务器哦！");
            return;
        }

        wuZiQiClient = new WuZiQiClient();
        wuZiQiClient.readMeg.start();

        startGame = new StartGame(socket);


        strtForm = new StrtForm();


//        scoreboard = new Scoreboard();


        startGame.setStrtForm(strtForm);//让开始游戏可以返回主页
//        scoreboard.setStrtForm(strtForm);//让排行榜可以返回主页


        strtForm.setWuZiQiMain(wuZiQiClient);
        strtForm.setStartGame(startGame);
//        strtForm.setScoreboard(scoreboard);
//        strtForm.setMallForm(mallForm);
        strtForm.setStrtForm(strtForm);


        strtForm.setVisible(true);//开始界面

        wuZiQiClient.setVisible(false);

        startGame.setVisible(false);
//        scoreboard.setVisible(false);


    }


    public void run() {//
        String[][] roomMeg = null;
        boolean threadK = true;

        while (threadK) {
            try {

                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String reSu = in.readLine();
                System.out.println(reSu);
                String[] reList = reSu.split("#");
                for (String ss : reList) {
                    System.out.println(ss + "-------------------");
                }

//                0、创建房间 | 房间名字 | 房主名字 |房间人数
//                1、加入房间 | 房间名字 | 玩家名字
//                2、离开房间 | 房间名字 | 玩家名字
//                3、刷新房间 | 房间名字 | 房间人数
//                4、刷新本房间 | 房间名字 | 房间人数 |房间玩家名字···
//                5、道具使用 |房间名字 | 玩家名称 | 玩家名称| 玩家名称```
//                6、指定玩家可以下棋 | 房间名字 | 玩家序号


                switch (Integer.parseInt(reList[0])) {

                    case 0:

                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3: //3、刷新房间 | 房间名字 | 房间人数


//                        String [][]roomMeg=new String[][];


                        if (reList.length == 1) {
                            roomMeg = null;
                            startGame.refreshTable(roomMeg);
                            JOptionPane.showMessageDialog(null, "没有人创建了房间哦！");
//                            startGame.
//                            roomMeg=null;
//                            startGame.refreshTable(roomMeg);
                        } else {
                            String room[] = reList[1].split("/");//分割房间消息


                            roomMeg = new String[room.length][2];

                            for (int iR = 0; iR < room.length; iR++) {
//                                System.out.println("房间数据:" + room[iR] + " " + room.length);
                                String roomTem[] = room[iR].split("@");//分割房间名称、人数
                                roomMeg[iR][0] = roomTem[0];
                                roomMeg[iR][1] = roomTem[1];
                            }

                            startGame.refreshTable(roomMeg);
                        }
                        break;
                    case 4:
                        String roomView[] = reList[1].split("/");//分割房间消息
                        String[][] roomViewMeg = new String[roomView.length][2];

                        for (int iR = 0; iR < roomView.length; iR++) {
//                            System.out.println("房间玩家数据:" + roomView[iR] + " " + roomView.length);
                            if (iR == roomView.length) break;
                            String roomTem[] = roomView[iR].split("@");//分割房间名称、人数
                            roomViewMeg[iR][0] = roomTem[0];
                            roomViewMeg[iR][1] = roomTem[1];
                        }

                        startGame.refreshTableView(roomViewMeg);
                        break;
                    case 5://
                        String allName[] = reList[2].split("/");
                        Map<String, Integer> mapTem = new HashMap<String, Integer>();
                        for (int i = 0; i < allName.length; i++) {
                            mapTem.put(allName[i], i);
//                            System.out.println("入局玩家名称：" + allName[i]);
                        }
                        chessBoard.setAllPlayName(mapTem);//把玩家名称添加到棋盘

                        //开始选择道具
                        startGame.setVisible(false);
                        wuZiQiClient.setVisible(true);
                        chessBoard.setVisible(false);
                        new Thread() { //这样的方式开线程不需要去继承Thread类
                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                for (int i = 15; i >= 0; i--) {
                                    label1TimeText.setText("即将开始：" + i + "s");
                                    try {
                                        sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                                panelProp.setVisible(false);
                                chessBoard.setVisible(true);
                            }

                        }.start();

                        chessBoard.setRoomName(reList[1]);//
                        chessBoard.setUserName(startGame.getUserName());
                        chessBoard.setPlayChessColor(mapTem.get(startGame.getUserName()));
                        yuanButton.setText(ColorName[mapTem.get(startGame.getUserName())]);//设置按钮文字
                        String[][] roomRe = null;
//                        startGame.refreshTable(roomRe);
                        startGame.refreshTableView(roomRe);

//                        panelProp.setVisible(false);
//                        chessBoard.setVisible(true);
                        chessBoard.setBlackTime(15);

                        break;
                    case 6://更新持手
                        chessBoard.setCurrentChessColor(Integer.parseInt(reList[2]));//设置当前下棋的颜色
                        chessBoard.setCurrenPlay(Integer.parseInt(reList[2]));//设置当前可以下棋的玩家
                        chessBoard.setBlackTime(15);
                        panelProp.setOpaque(false);

                        //更新按钮
                        killButton.setText("灭棋x" + chessBoard.getKillNum());
                        discolorButton.setText("变色棋x" + chessBoard.getDisNum());
                        bombButton.setText("炸棋x" + chessBoard.getBombNum());


                        break;

                    case 7://刷新棋盘
                        love.yuanmengrobot.wuziqi.Util.Point point = null;
                        String[] plist = reList[5].split("@");
                        chessArrayList = new ArrayList<love.yuanmengrobot.wuziqi.Util.Point>();
                        for (String p : plist) {
//                            System.out.println("第一次分割结果：" + p);
                            String pp[] = p.split("/");
                            point = new love.yuanmengrobot.wuziqi.Util.Point(Integer.parseInt(pp[0]), Integer.parseInt(pp[1]), colorSen[Integer.parseInt(pp[2])]);
                            chessArrayList.add(point);
                        }
                        chessBoard.setCurrenPlay(Integer.parseInt(reList[2]));//设置当前可用下棋的玩家
                        chessBoard.setBlackTime(15);
                        chessBoard.setChessArrayList(chessArrayList);

//                        ;
//                        chessBoard.getAllPlayName();
                        String score = reList[3].replace("{", "").replace("}", "");
//                        System.out.println("积分：" + score);
                        String playScore[] = score.split(", ");
//                        Label label = new Label("  ");
                        String[] colorname = {"红色", "橙色", "金色", "绿色", "蓝色", "靛色", "紫色"};
                        for (int i = 0; i < chessBoard.getAllPlayName().size(); i++) {
                            for (int j = 0; j < colorname.length; j++) {
                                if (playScore[j].contains(colorname[i])) {
//                                    System.out.println("颜色遍历" + playScore[j] + " " + colorname[i]);
                                    switch (i) {
                                        case 0:
                                            player0name.setText("    	 " + playScore[j] + "    ");
                                            mapScore.put(i, Integer.parseInt(playScore[j].substring(3)));//分别记录序号、分数
                                            break;
                                        case 1:
                                            player1name.setText("    	 " + playScore[j] + "    ");
                                            mapScore.put(i, Integer.parseInt(playScore[j].substring(3)));//分别记录序号、分数
                                            break;
                                        case 2:
                                            player2name.setText("    	 " + playScore[j] + "    ");
                                            mapScore.put(i, Integer.parseInt(playScore[j].substring(3)));//分别记录序号、分数
                                            break;
                                        case 3:
                                            player3name.setText("    	 " + playScore[j] + "    ");
                                            mapScore.put(i, Integer.parseInt(playScore[j].substring(3)));//分别记录序号、分数
                                            break;
                                        case 4:
                                            player4name.setText("    	 " + playScore[j] + "    ");
                                            mapScore.put(i, Integer.parseInt(playScore[j].substring(3)));//分别记录序号、分数
                                            break;
                                        case 5:
                                            player5name.setText("    	 " + playScore[j] + "    ");
                                            mapScore.put(i, Integer.parseInt(playScore[j].substring(3)));//分别记录序号、分数
                                            break;
                                        case 6:
                                            player6name.setText("    	 " + playScore[j] + "    ");
                                            mapScore.put(i, Integer.parseInt(playScore[j].substring(3)));//分别记录序号、分数
                                            break;
                                    }
                                }
                            }
                        }
//                        System.out.println("游戏结束标志：" + reList[4]);
                        if (reList[4].contains("true")) {
                            System.out.println("游戏结束!");
                            chessBoard.setGameOver(true);
                            //积分插入数据库，参数：userid
//                            显示结果
//
//                            chessBoard.getAllPlayName()
                            Object[][] dataObjects = new Object[chessBoard.getAllPlayName().size()][2];
                            int i = 0;
                            int keynum = 0;
                            for (Map.Entry<String, Integer> entry : chessBoard.getAllPlayName().entrySet()) {
                                if (entry.getKey().equals(startGame.getUserName())) keynum = i;
                                dataObjects[i][0] = entry.getKey();//key是玩家名字
                                dataObjects[i][1] = mapScore.get(entry.getValue());//value是玩家序号，通过序号获取分数
//                                System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue() + "  |" + mapScore.get(entry.getValue()));
                                i++;
                            }
                            String resultScore = dataObjects[keynum][1].toString();
                            ScoreWin scoreWin = new ScoreWin(dataObjects);
                            scoreWin.updataUserScore(startGame.getUserLoginId(), resultScore);

                            while (!scoreWin.isAck()) {
                                System.out.println("等待确认中");
                                try {
                                    System.out.println("休息···");
                                    sleep(1500);
                                    dispose();//关闭窗体
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            wuZiQiClient.setVisible(false);
                            wuZiQiClient = new WuZiQiClient();//刷新棋盘
                            strtForm.setWuZiQiMain(wuZiQiClient);
                            Thread readMegNew = new Thread(wuZiQiClient);//倒计时线程
                            readMegNew.start();

//                            String meg = "3#";//刷新房间指令
//                            sendMegToOne(meg, out);

                            startGame.backHome();
                            startGame.setVisible(true);
                            String[][] room = null;
                            startGame.refreshTable(room);
                            startGame.refreshTableView(room);
                            threadK = false;
                        }

//                        chessBoard.setGameOver();


                        break;

                }


            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}

